
"""
    多线程之间共享变量 加锁  互斥锁
"""
import threading
import time


def work1():

    global num
    for i in range(1000000):
        lock1.acquire()
        num += 1
        lock1.release()

    print("work1----", num)


def work2():
    global num
    for i in range(1000000):
        lock1.acquire()
        num += 1
        lock1.release()

    print("work2----", num)


if __name__ == '__main__':

    # 创建一把锁
    lock1 = threading.Lock()
    num = 0

    thread1 = threading.Thread(target=work1)
    thread2 = threading.Thread(target=work2)

    thread1.start()
    thread2.start()

    while len(threading.enumerate()) != 1:
        time.sleep(1)

    print("num = %d" % num)


